package com.jiang.mall.mallorder.config;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Configuration
public class MyRabbitConfig {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @PostConstruct // 在构造函数之后执行这个方法，构造函数最后生成，也就是这个类生成之后？
    public void initRabbitTemplate() {
        // spring.rabbitmq.publisher-confirms=true
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            /**
             * 只要消息抵达 broker(交换机)，b=true
             * @param correlationData
             * @param b
             * @param s
             */
            @Override
            public void confirm(CorrelationData correlationData, boolean b, String s) {
//                System.out.println("消息抵达broker,触发回调！===>CorrelationData" + correlationData.getId());
            }
        });
        // spring.rabbitmq.publisher-returns=true
        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int i, String s, String s1, String s2) {
//                System.out.println("消息抵达队列，出现失败情况的回调！replyText===>" + message + ",replyCode===>" + returnedMessage.getReplyCode());
            }
        });
    }

}




